var modifyId=0;

$(function(){
	YuXi.util.post("userInfoUpQuery.do",null,function(){
		
	});
	modifyId=queryString('detailId');	
	getstru();
	getPosition();
	getUser(modifyId);
	var checkFlag={department:false,child:false,persRole:false,roleList:false};
	$(document).on("change","#user-fileUp-img",function(){		
		fileup ();
	});
	$(".prison").click(function() {
		$(".child").html("");
		str = '<option value="choice">请选择分监区</option>';
		$(".child").append(str);
		$(".department").html("");
		str = '<option value="choice">请选择科室或者监区</option>';
		$(".department").append(str);
		getstru();
	});
	$("#goback").click(function(){
		window.history.back();
	})
	/*$("#user-headerImg").click(function(){
		$("#user-fileUp-img").click();
	});*/
	//$("#user-fileUp-img").change
	$(".department").change(function(){
		$(".child").html("");
		str='<option value="choice">请选择分监区</option>';
		$(".child").append(str);
		var structureId = $(this).val();
		if( structureId !="choice"){
			
			if($(".department :selected").attr("data-num")=='0'){
				getAllStructureBySuperId(structureId);
			}
		}
	});
	function getAllStructureBySuperId(structureId){
		YuXi.util.get("getAllStructureBySuperId.do",{superId:structureId},function(data){
			var str="";
			var object=data.object;
			$(".child").html("");
			str='<option value="choice">请选择分监区</option>';
			$(".child").append(str);
			for(var i=0;i<object.length;i++){
				str='<option value="'+object[i].id+'">'+object[i].name+'</option>';
				$(".child").append(str);	
			}
				
		},function(){
			
		});
	}
	//职务
	function getPosition(){
		var url="getAllPsoition.do";//根据部门获取角色地址
		YuXi.util.get(url,"null",function(data){
			var str="";
			var object=data.object;
			for(var i=0;i<object.length;i++){
				str='<option value="'+object[i].id+'">'+object[i].positionName+'</option>';
				$(".position").append(str);	
			}
			
		},function(){
			
		})
	}
	 function fileup (){
		
		 YuXi.loadding.show();
	    $.ajaxFileUpload({
	        type:"post",
	        secureuri:false,
	        dataType:"json",      
	        url:"/yuxi/userFileUp.do",
	        fileElementId:"user-fileUp-img",
	        success:function(data, status){	        	        	
	        	$("#user-headerImg").attr("src",data.addressList[0]);
	        	$("#user-fileUp-img").parent().html('<input style="width: 180px;height: 220px;position: absolute; right: 0; top: 0; font-size: 14px; opacity: 0; filter: alpha(opacity = 0);" type="file" id="user-fileUp-img" name="files" ></input>');
	        	YuXi.loadding.hide();
	        },
	        error:function(data){
	        	
	        	YuXi.loadding.hide();
	        } 
	        
	      });
	    }

	



function getstru()
{	
	var url="getDepartmentAndArea.do";//获取部门
	var data={};
	var object={};
	data=YuXi.util.get(url,null,function(data){
		object=data.object;
		for(var i=0;i<object.length;i++)
		{
			var html='<option  data-num='+object[i].num+' value="'+object[i].id+'" data-id="'+object[i].id+'">'+object[i].name+'</option>';
			$(".department").append(html);
		}
		
		
	},function(){});	
}
function getUser(modifyId){
	YuXi.util.get("getUserById.do",{userId:modifyId},function(data){
		if(data.status){
			getUserDetail(data.object)
		}
	});	
}

//正则取链接传来的数据
function queryString(key)
{
    var regex_str = "^.+\\?.*?\\b"+ key +"=(.*?)(?:(?=&)|$|#)";
    var regex = new RegExp(regex_str,"i");
    var url = window.location.toString();
    if(regex.test(url)) return RegExp.$1;
    return undefined;
}
/*根据userList传来的id查询当前用户数据*/
function getUserDetail(object){	
	////console.log("ooooo==="+JSON.stringify(object));
	$("input[name=username]").val(object.userName);
	$("input[name=accout]").val(object.userAccount);
	$("input[name=identity]").val(object.idNumber);
	$("input[name=phone]").val(object.phone);
	$("#user-headerImg").attr("src",object.userHeadPortait);
	$(".struc").find("option[value="+object.structId+"]").attr("selected",true);
	getPositionAndSelect(object.structId,object.positionId);
	getRole(object.roleId);
	$(".department").find("option[value="+object.structId+"]").attr("selected",true);
	
	var flag=true;
	if(object.structId!=1){
		$(".department option").each(function(){
			if($(this).val()==object.structId){
				$(this).attr("selected",true);
				flag=false;
			}
		});
		if(flag){
			getStructureById(object.structId);
		}
	}
	addSelect(".department",object.structName);
	addSelect(".child",object.structName);
	checkFlag.child=true;
	
	checkFlag.department=true;
	var s=$(".department").find("option:selected").val();
	if(s=='choice'){
		//checkFlag.department=false;
	}

}


var fff={department:false,child:false,persRole:false,roleList:false};
function addSelect(className,value){
	var length=className.length;
	
	var newname=className.substring(1,length);
	
	var time=setTimeout(function(){
		//alert(checkFlag.persRole+checkFlag.roleList+checkFlag.child);
		if(fff.persRole==true && fff.roleList==true && fff.child==true)
		{
			
			//clearInterval(time);
		}
		if(checkFlag[newname]==true)
		{

			$(className+" option").each(function(){
			if($(this).html()==value){
				$(this).attr("selected","selected");

				if(className==".department")
				{
					
					$(".select2-selection__rendered").eq(1).attr("title",value);
					$(".select2-selection__rendered").eq(1).html(value);
					fff.department=true;
				}
				else if(className==".child")
				{
					$(".select2-selection__rendered").eq(2).attr("title",value);
					$(".select2-selection__rendered").eq(2).html(value);
					fff.child=true;
				}else if(className==".persRole")
				{
					$(".select2-selection__rendered").eq(3).attr("title",value);
					$(".select2-selection__rendered").eq(3).html(value);
					fff.persRole=true;
				}
				else if(className=='.roleList')
				{
					$(".select2-selection__rendered").eq(4).attr("title",value);
					$(".select2-selection__rendered").eq(4).html(value);
					fff.roleList=true;
				}
				
			}
			})
		}
		
	},2000);
	
}



//保存数据模态框
$("#saveModal").on('click',function(){
	$("#modifyModel").modal('show');
});

/*根据部门id获取角色*/
function getPositionAndSelect(rid,backFunction){

	var url="getRoleByStructureId.do";//根据部门获取角色地址
	YuXi.util.get(url,{structureId:rid},function(data){
		var str="";
		var object=data.object;
		$(".persRole").html("");	
		for(var i=0;i<object.length;i++){
			str='<option value="'+object[i].id+'">'+object[i].positionName+'</option>';
			$(".persRole").append(str);	
		}
		
		var htm=$(".persRole").find("option[value="+backFunction+"]").text();
		//$("#select2-5b96-container").selected='selected';
		checkFlag.persRole=true;	
			
		addSelect(".persRole",htm);
		//console.log("获取角色成功");4
	},function(){
		//console.log("获取角色失败");
	})
	
	
}
function getRole(roleId){
	
	var url="getAllRole.do";//根据部门获取角色地址
	YuXi.util.get(url,null,function(data){
		var str="";
		var object=data.object;
		$(".roleList").html("");

		for(var i=0;i<object.length;i++){
			str='<option value="'+object[i].id+'">'+object[i].roleName+'</option>';
			$(".roleList").append(str);	
		}
		var htm=$(".roleList").find("option[value="+roleId+"]").text();
		
		checkFlag.roleList=true;
		
		addSelect(".roleList",htm);
		//console.log("获取角色成功");
	},function(){
		//console.log("获取角色失败");
	})
}

function getStructureById(id){
	var url="getPrisonStructureById.do";//根据部门获取角色地址
	YuXi.util.get(url,{id:id},function(data){
		var object=data.object;

		if(data.status){
			
			YuXi.util.get("getAllStructureBySuperId.do",{superId:object.superId},function(data){
				var str="";
				var object=data.object;
				//console.log(JSON.stringify(object));
				$(".child").html("");
				str='<option value="choice">请选择分监区</option>';
				$(".child").append(str);
				for(var i=0;i<object.length;i++){
					if(object[i].id==id){
						str='<option selected="selected" value="'+object[i].id+'">'+object[i].name+'</option>';

					}else{
						str='<option value="'+object[i].id+'">'+object[i].name+'</option>';
					}
					
					$(".child").append(str);	
				}
				$(".department").find("option[value="+object[0].superId+"]").attr("selected",true);
				
				var htm=$(".department").find("option[value="+object[0].superId+"]").html();
				addSelect(".department",htm);
				checkFlag.department=true;
				
			},function(){
				
			});
		}
		
	},function(){
		
	})
}

//保存修改后的数据
$("#saveModBtn").on('click',function(){
	$("#modifyModel").modal('hide');
	var modiUser={};
	modiUser.userName=$("input[name=username]").val();
	modiUser.userAccount=$("input[name=accout]").val();
	modiUser.idNumber=$("input[name=identity]").val();
	modiUser.phone=$("input[name=phone]").val();
	modiUser.positionId=$('.persRole option:selected').val();
	modiUser.positionName=$('.persRole option:selected').text();
	modiUser.roleId=$('.roleList :selected').val();
	modiUser.userHeadPortait=$("#user-headerImg").attr("src");
	modiUser.userId=modifyId;
	modiUser.userPassword=$("input[name=password]").val();

	  if($(".child").val()!="choice"){
		  modiUser.structureId =$(".child :selected").val();
		  modiUser.structureName=$(".child :selected").text();
	  }else if($(".department").val()!="choice"){
		  modiUser.structureName=$(".department :selected").text();
		  modiUser.structureId =$(".department :selected").val();
		
	  }else{
		 
		  modiUser.structureId =1;
		  modiUser.structureName="监狱";
	  }
	
	  
	YuXi.util.post("modifyUser.do",modiUser,function(data){
		if(data.status){
			myTool.alert({
				title:data.subType,
				context:data.msg
			});
			setTimeout(function(){
				 window.location.href='userList.jsp?menuId=25';
			},500);
		}
		myTool.alert({
			title:data.subType,
			context:data.msg
		});
	});
});



});